using System.IO;
using System.Linq;
using TripleA.Core.Interfaces.Entities;
using TripleA.Core.Interfaces.Exceptions;
using Path = Fluent.IO.Path;

namespace TripleA.Core.Extensions.FileSystem
{
    public class FolderContainsFileStep<T> : StepBase<T>
        where T: Context
    {
        public string Folder { get; set; }
        public string Filename { get; set; }
        public bool ExpectedOutcome { get; set; }

        public FolderContainsFileStep(string folder, string filename, bool expectedOutcome = true)
        {
            Folder = folder;
            Filename = filename;
            ExpectedOutcome = expectedOutcome;
        }

        public override void Execute(T context)
        {
            var path = Path.Get(Folder);

            if (!path.Exists)
                throw new DirectoryNotFoundException(string.Format("Folder '{0}' does not exist", Folder));

            if (path.Files(Filename, false).Any() != ExpectedOutcome)
                throw new ExpectationException("File '{0}' in '{1}' should {2}exist", Filename, Folder, 
                    ExpectedOutcome ? string.Empty : "not ");
        }
    }
}